replace()

Read(26) Label: replace,

Here are how to use replace() functions.

replace(s,a,b)

Description:

Replace a specified substring of a source string.

Syntax:

replace (s,a,b)

Note:

The function replaces substring a of string s with string b. When parameters a and b are sequences, replace members of a with corresponding members of b.

Parameter:

s

Source string.

a

A substring of the source string, or a sequence of substrings.

b

The string or a sequence of strings with which the specified substring will be replaced.

Option:

@q

A quoted specified string won’t be replaced.

@1

Enable to replace the first-found specified substring only.

@c

Case-insensitive.

@s

Split content of a and b respectively into a sequence and perform the replacement correspondingly.

@w

Only replace the substring with the same number of characters.

Return value:

String

Example:

replace("123321","2","two")

1two33two1.

replace("abc'abc'def","a","China")

Chinabc'Chinabc'def.

replace@q("abc'abc'def","a","China")

Chinabc'abc'def; the “a” enclosed by the single quotes are not replaced.

replace@1("abcabcdef","a","China")

Chinabcabcdef; only the first “a” is replaced.

replace@c("abcabcdef","A","China")

ChinabcChinabcdef; case-insensitive.

replace("1212341",["1","2"],["8","9"])

8989348.

replace@s("1212341","12","89")

8989348.

replace("aaa,aab,aa","aa","AA")

Return AAa,AAb,AA.

replace@w("aaa,aab,aa","aa","AA")

Use @w option to replace the substring having the same number of characters only; here the function returns aaa,aab,AA:

replace(s,p:l,b)

Description:

Replace a substring at the specified position in a source string.

Syntax:

replace(s,p:l,b)

Note:

The function replaces a substring starting from position p and of the length of l with string b. When parameter l is absent or 0, insert string b at position p; when parameter b is absent, delete a number of (l) characters at position p.

 

When parameter p is 0 or greater than the length of source string s, perform the replacement at the end of the source string.

Parameter:

s

Source string.

p

An integer.

l

A positive integer or 0.

b

The replacement string.

Return value:

String

Example:

replace("123456",2:3,"abc")

Starting from the 2nd character counted from the leftmost, replace the three continuous characters with abc, and return 1abc56.

replace("123456",-3:1)

Starting from the 3rd character counted from the rightmost, delete one character, and return 12356.

replace("123456",4,"abc")

Insert characters abc before the 4th character counted from the leftmost, and return 123abc456.

replace("123456",4:0,"abc")

Same as above.

replace("123456",-2,"abc")

Insert characters abc before the 2nd character counted from the rightmost, and return 123abc56.

replace("123456",0,"abc")

Insert characters abc at the end of the source string, and return 123456abc.